package cn.tedu.autoshow.passport.controller;

import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.web.JsonResult;
import cn.tedu.autoshow.passport.pojo.param.RoleAddNewParam;
import cn.tedu.autoshow.passport.pojo.param.RoleUpdateParam;
import cn.tedu.autoshow.passport.pojo.vo.RoleListItemVO;
import cn.tedu.autoshow.passport.pojo.vo.RoleStandardVO;
import cn.tedu.autoshow.passport.service.IAdminRoleService;
import cn.tedu.autoshow.passport.service.IRolePermissionService;
import cn.tedu.autoshow.passport.service.IRoleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/roles")
@Api(tags = "02. 角色管理模块")
@Slf4j
@Validated
public class RoleController {

    @Autowired
    private IRoleService service;
    @Autowired
    private IAdminRoleService adminRoleService;
    @Autowired
    private IRolePermissionService rolePermissionService;
    
    @PostMapping("/add-new")
    @ApiOperation("添加角色")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid RoleAddNewParam roleAddNewParam){
        service.addNew(roleAddNewParam);

        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除角色")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long")
    })
    public JsonResult deleteById(
            @Range(min = 1, message = "根据ID删除角色失败，请提交合法的ID值！")
            @PathVariable Long id){
        service.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("根据ID更新角色")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long")
    public JsonResult updateById(
            @Range(min = 1, message = "根据ID更新角色失败，请提交合法的ID值！")
            @PathVariable Long id,
            @Valid RoleUpdateParam roleUpdateParam){
        service.updateInfoById(id, roleUpdateParam);

        return JsonResult.ok();
    }

    @PostMapping("/{roleId:[0-9]+}/insertsAdminRole")
    @ApiOperation("配置管理员角色-添加管理员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long")
    })
    public JsonResult insertsAdminRole(
            @PathVariable @Range(min = 1, message = "请提交有效的角色ID值！") Long roleId,
            Long[] adminIds) {
        adminRoleService.insertsAdminRoleByRoleId(roleId, adminIds);
        return JsonResult.ok();
    }

    @PostMapping("/{roleId:[0-9]+}/deletesAdminRole")
    @ApiOperation("配置管理员角色-解除管理员")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long")
    })
    public JsonResult deletesAdminRole(
            @PathVariable @Range(min = 1, message = "请提交有效的角色ID值！") Long roleId,
            Long[] adminIds) {
        adminRoleService.deletesAdminRoleByRoleId(roleId, adminIds);
        return JsonResult.ok();
    }

    @PostMapping("/{roleId:[0-9]+}/insertsRolePermission")
    @ApiOperation("配置角色权限-添加权限")
    @ApiOperationSupport(order = 312)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long")
    })
    public JsonResult insertsRolePermission(
            @PathVariable @Range(min = 1, message = "请提交有效的角色ID值！") Long roleId,
            Long[] permissionIds) {
        rolePermissionService.insertsRolePermissionByRoleId(roleId, permissionIds);
        return JsonResult.ok();
    }

    @PostMapping("/{roleId:[0-9]+}/deletesRolePermission")
    @ApiOperation("配置角色权限-解除权限")
    @ApiOperationSupport(order = 313)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "long")
    })
    public JsonResult deletesRolePermission(
            @PathVariable @Range(min = 1, message = "请提交有效的角色ID值！") Long roleId,
            Long[] permissionIds) {
        rolePermissionService.deletePermissionByRole(roleId, permissionIds);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询角色详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "Long")
    })
    public JsonResult getStandardById(
            @Range(min = 1, message = "根据ID查询角色失败，请提交合法的ID值！")
            @PathVariable Long id){
        RoleStandardVO standardById = service.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("/{roleId:[0-9]+}/admins")
    @ApiOperation("根据ID查询角色下有多少管理员")
    @ApiOperationSupport(order = 411)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Long")
    })
    public JsonResult adminListByRoleId(
            @Range(min = 1, message = "根据ID查询角色失败，请提交合法的ID值！")
            @PathVariable Long roleId){
        List<Long> longs = adminRoleService.adminIdListByRoleId(roleId);
        return JsonResult.ok(longs);
    }

    @GetMapping("/{roleId:[0-9]+}/permissions")
    @ApiOperation("根据ID查询角色下有多少权限")
    @ApiOperationSupport(order = 413)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "Long")
    })
    public JsonResult permissionListByRoleId(
            @Range(min = 1, message = "根据ID查询角色失败，请提交合法的ID值！")
            @PathVariable Long roleId){
        List<Long> longs = rolePermissionService.permissionListByRoleId(roleId);
        return JsonResult.ok(longs);
    }

    @GetMapping("")
    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码值", paramType = "query"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult list(
            @Range(min = 1, message = "查询角色列表失败，请提供正确的页码值!") Integer pageNum,
            String queryType){
        if(pageNum == null || pageNum < 1){
            pageNum = 1;
        }
        PageData<RoleListItemVO> pageData ;
        if("all".equals(queryType)){
            pageData = service.list(pageNum, Integer.MAX_VALUE);
        }else{
            pageData = service.list(pageNum);
        }

        return JsonResult.ok(pageData);
    }

}
